package 爆刷leetcode.day0615;

/**
 * @Author yaya
 * @Create 2024/6/15 10:13
 * Description:删除链表中某个节点
 */
public class Code0136_deleteNode {
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

    public ListNode deleteNode(ListNode head, int val) {
        ListNode temp = head;
        ListNode pre = null;
        while (temp != null){
            if (temp.val == val){
                if (temp == head){
                    return head.next;
                }else if (temp.next == null){
                    pre.next = null;
                }else {
                    pre.next = temp.next;
                    temp.next = null;
                }
            }
            pre = temp;
            temp = temp.next;
        }
        return head;
    }
}
